# -*- coding:utf8 -*-

import MySQLdb

class WeatherPipeline(object):
    def process_item(self, item, spider):
        print '-----------------mysql-------------------'
        print item['week'].encode('utf8')
        week = item['week'].encode('utf8')
        weather = item['weather'].encode('utf8')
        temperature = ''
        temperature += item['temperature'][0].encode('utf8')
        temperature += '℃~'
        temperature += item['temperature'][1].encode('utf8')
        wind = item['wind'].encode('utf8')

        conn = MySQLdb.connect(
        host = '127.0.0.1',
        port = 3306,
        user = 'root',
        passwd = 'xhq021620',
        db = 'spider_data',
        charset = 'utf8'
        )

        cur = conn.cursor()
        try:
            cur.execute('''insert into weather (week, weather,
            temperature, wind) values (%s, %s, %s, %s)''',
            (week, weather,temperature, wind));
        except MySQLdb.Error as err:
            print 'write to database error'
            print str(err)



        cur.close()
        conn.commit()
        conn.close()

        print '-----------------mysql-------------------'
        return item
